<template>
  <div>
    <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
        <el-row>
            <el-col>
                <el-button type="primary">添加角色</el-button>
            </el-col>
        </el-row>
        <el-table :data="roleList" style="width: 100%" border stripe v-loading="tableLoading">
            <el-table-column type="expand">                
                <template scope="scope">
                    <el-row :class="['bdbottom', i1 ===0 ? 'bdtop':'','v-center']" v-for="(item1, i1) in scope.row.children" :key="item1.id">
                        <!-- 一级权限 -->
                        <el-col :span="5">
                            <el-tag closable >{{ item1.authName }}</el-tag>
                            <i class="el-icon-caret-right"></i>
                        </el-col>
                        <!-- 二级 -->
                        <el-col :span="19">
                            <el-row :class="[i2===0?'':'bdtop','v-center']" v-for="(item2 ,i2) in item1.children" :key="item2.id">
                                <el-col :span="6">
                                    <el-tag closable type="success">{{ item2.authName }}</el-tag>
                                    <i class="el-icon-caret-right"></i>
                                </el-col>
                                <el-col :span="18">
                                    <el-tag @close="removeRightById(item3.id)" closable type="warning" v-for="(item3,i3) in item2.children" :key="item3.id">{{ item3.authName }}</el-tag>
                                </el-col>
                            </el-row>
                        </el-col>
                    </el-row>
                </template>
            </el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column prop="roleName" label="角色名称"></el-table-column>
            <el-table-column prop="roleDesc" label="角色说明"></el-table-column>
            <el-table-column prop="level" label="等级" width="300px">
              <template scope="scope">
                <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
                <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
                <el-button @click="showSetRightDialog(scope.row)" size="mini" type="warning" icon="el-icon-setting">分配权限</el-button>
              </template>
            </el-table-column>            
        </el-table>
    </el-card>
    <!-- 分配权限 -->
    <el-dialog title="分配权限" :visible.sync="setRightDialogRightVisable" width="50%" @close="setRightDialogClose">
        <!-- 内容主题区 -->
        <el-tree :default-checked-keys="defkeys" show-checkbox :data="rightsList" :props="treeProps" node-key="id" default-expand-all ></el-tree>
        <!-- 底部按钮 -->
        <span slot="footer" class="dialog-footer">
            <el-button @click="setRightDialogRightVisable = false">取 消</el-button>
            <el-button type="primary" @click="dialogAddUser = false">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
    name: 'Roles',
    mixins: [],
    components: {},
    props: {},
    data () {
        return {
            roleList: [],
            tableLoading: false,
            setRightDialogRightVisable: false,
            rightsList: [],
            treeProps: {
                children: 'children',
                label: 'authName'
            },
            defkeys:[]
        }
    },
    created() {
        this.getRoleList()
    },
    mounted () {},
    methods: {
        getRoleList() {
            this.tableLoading = true
            this.http.get('/roles/list').then((res) => {
                this.tableLoading = false
                if (res.data.code === 200) {
                    this.roleList = res.data.data
                }
            }).catch((err) => {
                this.tableLoading = false
            })
        },
        removeRightById(id) {
            this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$message({
                    type: 'success',
                    message: '删除了' + id
                });
            }).catch(() => {
                this.$message.info('已取消删除')         
            });
        },
        showSetRightDialog(role) {
            this.http.get('/rights/allRight').then((res) => {
                if (res.data.code === 200) {
                    this.rightsList = res.data.data
                    this.getLeafKeys(role,this.defkeys)
                }
            }).catch((err) => {

            })
            this.setRightDialogRightVisable = true
        },
        getLeafKeys(node, arr) {
            if (!node.children) {
                return arr.push(node.id)
            }
            node.children.forEach(item => {
                this.getLeafKeys(item,arr)
            });
        },
        setRightDialogClose() {
            this.defkeys = []
        }
    }
}
</script>

<style scoped lang="scss">
.el-tag{
  margin: 7px;
}
.bdtop{
    border-top: 1px solid #eee;
}
.bdbottom{
    border-bottom: 1px solid #eee;
}
.v-center{
    display: flex;
    align-items: center;
}
</style>
